namespace CISLib.Mail
{
    using CISLib;
    using OpenSmtp.Mail;
    using System;

    public class SendMailEx
    {
        private EmailPool _currentEmail = null;
        private MailMessage _currentMailMessage = null;

        private Smtp CreateSmtpServer(MailConfig configer)
        {
            string str;
            string smtpAuthMode = MailConfig.SmtpAuthMode;
            smtpAuthMode = (smtpAuthMode == string.Empty) ? "None" : smtpAuthMode;
            if (((str = smtpAuthMode) != null) 
                && (string.IsInterned(str) == "None"))
            {
                return new Smtp(MailConfig.SmtpHost, MailConfig.SmtpPort);
            }
            return new Smtp(MailConfig.SmtpHost, MailConfig.SmtpSender, MailConfig.SmtpPass, MailConfig.SmtpPort);
        }

        public void SendMail()
        {
            try
            {
                ManageLog.InsertLog(Guid.NewGuid(), "Mail", DateTime.Now, "Enter Send Mail...", "", "");
                MailConfig configer = MailConfig.GetInstance();
                Smtp smtpServer = this.CreateSmtpServer(configer);
                smtpServer.StartedMessageTransfer += new EventHandler(this.smtpServer_StartedMessageTransfer);
                smtpServer.EndedMessageTransfer += new EventHandler(this.smtpServer_EndedMessageTransfer);
                EmailPool[] emails = ManageMail.GetEmailsViaFlag(0, this.MailSendCountPerJob);
                for (int i = 0; (emails != null) && (i < emails.Length); i++)
                {
                    try
                    {
                        this._currentEmail = emails[i];
                        this._currentMailMessage = MailHelper.BuildMailMessageEx(configer, this._currentEmail);
                        smtpServer.SendMail(this._currentMailMessage);
                    }
                    catch (Exception ex)
                    {
                        ManageLog.InsertLog(Guid.NewGuid(), "Mail", DateTime.Now, "Error Send Mail... " + ex.ToString(), "", "");
                    }
                }
                ManageLog.InsertLog(Guid.NewGuid(), "Mail", DateTime.Now, "Exit Send Maild...", "", "");
            }
            catch (Exception ex)
            {
                ManageLog.InsertLog(Guid.NewGuid(), "Mail", DateTime.Now, "Error in timer_Elapsed... " + ex.ToString(), "", "");
            }
        }

        public void SendOneMail(EmailPool email)
        {
            try
            {
                ManageLog.InsertLog(Guid.NewGuid(), "Mail", DateTime.Now, "Enter SendOneMail(Apply)...", "", "");
                MailConfig configer = MailConfig.GetInstance();
                Smtp smtpServer = this.CreateSmtpServer(configer);
                smtpServer.StartedMessageTransfer += new EventHandler(this.smtpServer_StartedMessageTransfer);
                smtpServer.EndedMessageTransfer += new EventHandler(this.smtpServer_EndedMessageTransfer);
                if (email != null)
                {
                    try
                    {
                        this._currentEmail = email;
                        this._currentMailMessage = MailHelper.BuildMailMessageEx(configer, this._currentEmail);
                        smtpServer.SendMail(this._currentMailMessage);
                    }
                    catch (Exception ex)
                    {
                        ManageLog.InsertLog(Guid.NewGuid(), "Mail", DateTime.Now, "Error SendOneMail(Apply)... " + ex.ToString(), "", "");
                    }
                }
                ManageLog.InsertLog(Guid.NewGuid(), "Mail", DateTime.Now, "Exit SendOneMail(Apply)...", "", "");
            }
            catch (Exception ex)
            {
                ManageLog.InsertLog(Guid.NewGuid(), "Mail", DateTime.Now, "Error in SendOneMail(Apply)... " + ex.ToString(), "", "");
            }
        }

        private void smtpServer_EndedMessageTransfer(object sender, EventArgs e)
        {
            if (this._currentMailMessage != null)
            {
                ManageMail.RemoveEmailViaId(this._currentEmail.EmailId);
            }
        }

        private void smtpServer_StartedMessageTransfer(object sender, EventArgs e)
        {
        }

        public virtual int MailSendCountPerJob
        {
            get
            {
                return 3;
            }
        }

        public virtual long ScheduledMailInterval
        {
            get
            {
                return 0x2bf20L;
            }
        }
    }
}

